package com.boc.langxiang.ui.activity.adm;

import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.TabLayout;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ImageView;

import com.boc.langxiang.R;
import com.boc.langxiang.contract.adm.AdmSimpleContract;
import com.boc.langxiang.entity.events.AdmFollowEvent;
import com.boc.langxiang.entity.response.AdmDetailEntity;
import com.boc.langxiang.entity.response.AdmFollowContent;
import com.boc.langxiang.entity.response.PhotoEntity;
import com.boc.langxiang.manager.PicassoImageLoader;
import com.boc.langxiang.presenter.adm.AdmSimplePresenter;
import com.boc.langxiang.ui.activity.BaseActivity;
import com.boc.langxiang.ui.adapter.adm.ArtistIndexPagerAdapter;
import com.boc.langxiang.ui.fragment.adm.ArtistAboutFragment;
import com.boc.langxiang.ui.fragment.adm.ArtistArtsFragment;
import com.boc.langxiang.ui.fragment.adm.ArtistMessageFragment;
import com.boc.langxiang.ui.view.SongTextView;
import com.boc.langxiang.ui.view.roundedimageview.RoundedImageView;
import com.boc.langxiang.util.StringUtil;
import com.boc.langxiang.util.T;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 艺术家主页
 */
public class ArtistIndexActivity extends BaseActivity implements AdmSimpleContract.View {

    //    @BindView(R.id.backdrop)
//    ImageView mBackdrop;
    @BindView(R.id.toolbar)
    Toolbar mToolbar;
    @BindView(R.id.collapsing_toolbar)
    CollapsingToolbarLayout mCollapsingToolbar;
    @BindView(R.id.tabs)
    TabLayout mTabs;
    @BindView(R.id.appbar)
    AppBarLayout mAppbar;
    @BindView(R.id.viewpager)
    ViewPager mViewpager;
    @BindView(R.id.head)
    RoundedImageView mHead;
    @BindView(R.id.art_count)
    SongTextView mArtCount;
    @BindView(R.id.hot_count)
    SongTextView mHotCount;
    @BindView(R.id.name)
    SongTextView mName;
    @BindView(R.id.job)
    SongTextView mJob;
    private Menu mMenu;
    private AdmDetailEntity mAdmDetail;
    private AdmSimpleContract.Presenter mPresenter;
    private String mDesignerId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_artist_index);
        ButterKnife.bind(this);
        EventBus.getDefault().register(this);

        mToolbar.setTitle("");
        setSupportActionBar(mToolbar);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);

        Intent intent = getIntent();
        mDesignerId = intent.getStringExtra("designerId");

        if (mViewpager != null) {
            setupViewPager(mViewpager);
        }
        mTabs.setTabMode(TabLayout.MODE_FIXED);
        mTabs.setupWithViewPager(mViewpager);

        mPresenter = new AdmSimplePresenter(this);
    }

    private void initHeadview() {
        PhotoEntity photo = mAdmDetail.getPhoto();
        if (photo != null) {
            String url = photo.getUrl();
            PicassoImageLoader.getInstance().displayUrlImage(mContext, url, mHead);
        } else {
            PicassoImageLoader.getInstance().displayUrlImage(mContext, null, mHead);

        }

//        List<String> bannerurl = mAdmDetail.getBannerurl();
//        if (bannerurl != null && !bannerurl.isEmpty()) {
//            String s = bannerurl.get(0);
//            PicassoImageLoader.getInstance().displayUrlImage(mContext, s, mBackdrop);
//        } else {
//            PicassoImageLoader.getInstance().displayUrlImage(mContext, null, mBackdrop);
//        }

        mArtCount.setText(StringUtil.getValue(mAdmDetail.getProduct_num() + ""));
        mHotCount.setText(StringUtil.getValue(mAdmDetail.getClick_num() + ""));
        mHotCount.setText(StringUtil.getValue(mAdmDetail.getClick_num() + ""));
        mName.setText(StringUtil.getValue(mAdmDetail.getNickname()));
        mJob.setText(StringUtil.getValue(mAdmDetail.getZhiwu()));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        mMenu = menu;
        int click_type = AdmFollowContent.CANCEL;
        matchStar(click_type);
        //为了先让menu初始化完成
        mPresenter.admDetail(mDesignerId);
        return true;
    }

    private void matchStar(int start) {
        mMenu.clear();
        switch (start) {
            case AdmFollowContent.CANCEL:
                getMenuInflater().inflate(R.menu.menu_artist_star_kong, mMenu);
                break;
            case AdmFollowContent.SUCCESS:
                getMenuInflater().inflate(R.menu.menu_artist_star_solid, mMenu);
                break;
            case AdmFollowContent.FAIL:
                break;
        }

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
//        mMenu.clear();
        if (id == android.R.id.home) {
            finish();
        }
        if (id == R.id.action_star_kong || id == R.id.action_star_solid) {
            if (isLogin()) {
                mPresenter.admFollow(mDesignerId);
            }
            return true;
        }

        return super.onOptionsItemSelected(item);
    }


    private void setupViewPager(ViewPager viewpager) {
        ArtistIndexPagerAdapter adapter = new ArtistIndexPagerAdapter(getSupportFragmentManager());
        adapter.addFragment(ArtistAboutFragment.newInstance(mDesignerId), "关于");
        adapter.addFragment(ArtistArtsFragment.newInstance(mDesignerId), "作品集");
        adapter.addFragment(ArtistMessageFragment.newInstance(mDesignerId), "留言");
        viewpager.setAdapter(adapter);
    }


    @Override
    public void admDetailSuccess(AdmDetailEntity admDetail) {
        if (admDetail != null) {
            mAdmDetail = admDetail;
            initHeadview();
            matchStar(mAdmDetail.getClick_type());
        }

    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void onAdmFollowEvent(AdmFollowEvent event) {
        matchStar(event.type);
    }

    @Override
    public void showLoading() {
        showProgressDialog();
    }

    @Override
    public void hideLoading() {
        hideDialog();
    }

    @Override
    public void onError(String code, String msg) {
        T.showShort(mContext, msg);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
